import java.awt.BorderLayout;
import java.awt.CardLayout;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.ButtonGroup;
import javax.swing.JMenuBar;
import javax.swing.JMenu;
import javax.swing.JMenuItem;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.LayoutStyle.ComponentPlacement;
import javax.swing.JTextField;
import javax.swing.JRadioButton;
import javax.swing.JButton;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
//import java.security.cert.PKIXRevocationChecker.Option;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.swing.JComboBox;
import javax.swing.JSplitPane;
import javax.swing.JTabbedPane;
import java.lang.NumberFormatException;
import javax.swing.JCheckBox;


public class GUI extends JFrame {

        private JPanel     contentPane;
        private JTextField eventField;
        private JTextField startDateField;
        private JTextField endDateField;
        private JTextField locationField;
        private JTextField descriptionField;
        private String     privacyType = "";
        private String	   freqType = "";
        private String     recurrenceString = "";
        private String 	   intervalRec = "INTERVAL=";
        private String 	   dayRec = "BYDAY=";
        private int        priority = 0;
        private JTextField startTimeField;
        private JTextField endTimeField;
        private JComboBox  comboBox;
        private JTextField intervalTextField;
        private JCheckBox  sundayCheckBox, mondayCheckBox, tuesdayCheckBox, wednesdayCheckBox, thursdayCheckBox, fridayCheckBox, saturdayCheckBox;
        private JRadioButton rdbtnDaily, rdbtnWeekly, rdbtnMonthly, rdbtnYearly;
        private JTextField geoField;

        /**
         * Launch the application.
         */
        public static void main(String[] args) {
                EventQueue.invokeLater(new Runnable() {
                        public void run() {
                                try {
                                        GUI frame = new GUI();
                                        frame.setVisible(true);
                                } catch (Exception e) {
                                        e.printStackTrace();
                                }
                        }
                });
        }

        /**
         * Create the frame.
         */
        public GUI() {
                setTitle("Calendar GUI");
                setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                setBounds(100, 100, 610, 414);
               
                JMenuBar menuBar = new JMenuBar();
                setJMenuBar(menuBar);
               
                JMenu mnFile = new JMenu("File");
                menuBar.add(mnFile);
               
                JMenuItem mntmExit = new JMenuItem("Exit");
                mnFile.add(mntmExit);
                contentPane = new JPanel();
                contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
                setContentPane(contentPane);
               
                JLabel lblName = new JLabel("Event Name:");
               
                eventField = new JTextField();
                eventField.setColumns(10);
               
                startDateField = new JTextField();
                startDateField.setText("YYYYMMDD");
                startDateField.setColumns(10);
               
                JLabel lblNewLabel = new JLabel("to");
               
                endDateField = new JTextField();
                endDateField.setText("YYYYMMDD");
                endDateField.setColumns(10);
               
                JLabel lblNewLabel_1 = new JLabel("Where:");
               
                locationField = new JTextField();
                locationField.setColumns(10);
               
                JLabel lblDescription = new JLabel("Description:");
               
                descriptionField = new JTextField();
                descriptionField.setColumns(10);
               
                JLabel lblPrivacy = new JLabel("Privacy");
                              
                JRadioButton rdbtnPublic = new JRadioButton("Public");
                rdbtnPublic.setSelected(true);
                rdbtnPublic.addActionListener(new ActionListener() {
                        public void actionPerformed(ActionEvent arg0) {
                                privacyType = "PUBLIC";
                        }
                });
               
                JRadioButton rdbtnPrivate = new JRadioButton("Private");
               
                rdbtnPrivate.addActionListener(new ActionListener() {
                        public void actionPerformed(ActionEvent arg0) {
                                privacyType = "PRIVATE";
                        }
                });
                JRadioButton rdbtnConfidential = new JRadioButton("Confidential");
               
                rdbtnConfidential.addActionListener(new ActionListener() {
                        public void actionPerformed(ActionEvent arg0) {
                                privacyType = "CONFIDENTIAL";
                               
                        }
                });
                
                //Puts privacy types into a group so that you can't pick more than one type.
                ButtonGroup privacyGroup = new ButtonGroup();
                privacyGroup.add(rdbtnPublic);
                privacyGroup.add(rdbtnPrivate);
                privacyGroup.add(rdbtnConfidential);
               
                JButton btnNewButton = new JButton("Create File");
                btnNewButton.addActionListener(new ActionListener() {
                        public void actionPerformed(ActionEvent arg0) {
                                createFileButtonActionPerformed(arg0);
                        }
                });
               
               
                JLabel lblStartTime = new JLabel("Start Date");
               
                JLabel lblStartTime_1 = new JLabel("Start Time");
               
                JLabel lblEndDate = new JLabel("End Date");
               
                JLabel lblEndTime = new JLabel("End Time");
               
                String[] timeZone = {"(-10:00) Hawaiian Time", "(-9:00) Alaskan Time","(-8:00) Pacific Time","(-7:00) Mountain Time",
                                "(-6:00) Central Time","(-5:00) Eastern Time",};
               
                startTimeField = new JTextField();
                startTimeField.setText("HHMM");
                startTimeField.setColumns(10);
               
                endTimeField = new JTextField();
                endTimeField.setText("HHMM");
                endTimeField.setColumns(10);
                              
                comboBox = new JComboBox(timeZone);
               
                JLabel lblTimezone = new JLabel("TimeZone");
               
                JLabel lblCoordinates = new JLabel("Coordinates:");
               
                JLabel lblPriority = new JLabel("Priority");
               
                
                JRadioButton rdbtnHigh = new JRadioButton("High");
               
                rdbtnHigh.addActionListener(new ActionListener() {
                        public void actionPerformed(ActionEvent arg0) {
                                priority = 1;
                        }
                });
               
                JRadioButton rdbtnLow = new JRadioButton("Low");
               
                rdbtnLow.addActionListener(new ActionListener() {
                        public void actionPerformed(ActionEvent arg0) {
                                priority = 6;
                        }
                });
               
                ButtonGroup priorityGroup = new ButtonGroup();
                priorityGroup.add(rdbtnHigh);
                priorityGroup.add(rdbtnLow);
               
                //Main Buttons for Recurrence Patterns
                JLabel lblRecurrencePattern = new JLabel("Recurrence Pattern");
               
                rdbtnDaily = new JRadioButton("Daily");
                rdbtnDaily.addActionListener(new ActionListener(){
                    public void actionPerformed(ActionEvent arg0){
                        	freqType = "DAILY";
                    }
                });
               
                rdbtnWeekly = new JRadioButton("Weekly");
                rdbtnWeekly.addActionListener(new ActionListener(){
                	public void actionPerformed(ActionEvent arg0){
                			freqType = "WEEKLY";
                	}
                });
                rdbtnMonthly = new JRadioButton("Monthly");
                rdbtnMonthly.addActionListener(new ActionListener(){
            		public void actionPerformed(ActionEvent arg0){
            			freqType = "MONTHLY";
            		}
                });
                
                rdbtnYearly = new JRadioButton("Yearly");
                rdbtnYearly.addActionListener(new ActionListener(){
                	public void actionPerformed(ActionEvent arg0){
                		freqType = "YEARLY";
                	}
                });
               
                //Groups recurrence types into a group so you can't select more than one.
                ButtonGroup recurrenceGroup = new ButtonGroup();
                recurrenceGroup.add(rdbtnDaily);
                recurrenceGroup.add(rdbtnWeekly);
                recurrenceGroup.add(rdbtnMonthly);
                recurrenceGroup.add(rdbtnYearly);
                
                JLabel lblNewLabel_2 = new JLabel("Every");
                
                intervalTextField = new JTextField();
                intervalTextField.setColumns(10);
                
                JLabel lblNewLabel_3 = new JLabel("day(s)");
                
                sundayCheckBox    = new JCheckBox("S");
                mondayCheckBox    = new JCheckBox("M");              
                tuesdayCheckBox   = new JCheckBox("T");               
                wednesdayCheckBox = new JCheckBox("W");               
                thursdayCheckBox  = new JCheckBox("R");                
                fridayCheckBox    = new JCheckBox("F");              
                saturdayCheckBox  = new JCheckBox("ST");
                
                geoField = new JTextField();
                geoField.setText("83.6548;-122.8065");
                geoField.setColumns(10);
                
                JLabel lblLeaveBlankFor = new JLabel("(Leave blank for default priorty)");

               
                GroupLayout gl_contentPane = new GroupLayout(contentPane);
                gl_contentPane.setHorizontalGroup(
                	gl_contentPane.createParallelGroup(Alignment.LEADING)
                		.addGroup(gl_contentPane.createSequentialGroup()
                			.addGroup(gl_contentPane.createParallelGroup(Alignment.LEADING)
                				.addGroup(gl_contentPane.createSequentialGroup()
                					.addGap(10)
                					.addGroup(gl_contentPane.createParallelGroup(Alignment.LEADING)
                						.addGroup(gl_contentPane.createSequentialGroup()
                							.addComponent(lblName)
                							.addGap(4)
                							.addComponent(eventField, GroupLayout.PREFERRED_SIZE, 121, GroupLayout.PREFERRED_SIZE)
                							.addGap(41)
                							.addComponent(lblTimezone)
                							.addGap(4)
                							.addComponent(comboBox, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
                						.addGroup(gl_contentPane.createSequentialGroup()
                							.addGroup(gl_contentPane.createParallelGroup(Alignment.LEADING)
                								.addGroup(gl_contentPane.createSequentialGroup()
                									.addGroup(gl_contentPane.createParallelGroup(Alignment.LEADING)
                										.addComponent(lblDescription)
                										.addComponent(lblPrivacy)
                										.addComponent(lblNewLabel_1)
                										.addComponent(lblCoordinates))
                									.addPreferredGap(ComponentPlacement.RELATED)
                									.addGroup(gl_contentPane.createParallelGroup(Alignment.LEADING)
                										.addComponent(locationField, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
                										.addGroup(gl_contentPane.createSequentialGroup()
                											.addGap(2)
                											.addGroup(gl_contentPane.createParallelGroup(Alignment.LEADING)
                												.addGroup(gl_contentPane.createSequentialGroup()
                													.addComponent(rdbtnHigh)
                													.addGap(6)
                													.addComponent(rdbtnLow))
                												.addGroup(gl_contentPane.createSequentialGroup()
                													.addComponent(rdbtnPublic)
                													.addComponent(rdbtnPrivate)
                													.addComponent(rdbtnConfidential))))
                										.addComponent(descriptionField, GroupLayout.PREFERRED_SIZE, 188, GroupLayout.PREFERRED_SIZE)
                										.addComponent(geoField, GroupLayout.PREFERRED_SIZE, 160, GroupLayout.PREFERRED_SIZE)))
                								.addGroup(gl_contentPane.createSequentialGroup()
                									.addGroup(gl_contentPane.createParallelGroup(Alignment.LEADING)
                										.addGroup(gl_contentPane.createSequentialGroup()
                											.addPreferredGap(ComponentPlacement.RELATED)
                											.addComponent(startDateField, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
                										.addComponent(lblStartTime, GroupLayout.PREFERRED_SIZE, 62, GroupLayout.PREFERRED_SIZE))
                									.addGap(18)
                									.addGroup(gl_contentPane.createParallelGroup(Alignment.LEADING)
                										.addGroup(gl_contentPane.createSequentialGroup()
                											.addComponent(startTimeField, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
                											.addGap(18)
                											.addComponent(lblNewLabel, GroupLayout.PREFERRED_SIZE, 26, GroupLayout.PREFERRED_SIZE))
                										.addComponent(lblStartTime_1)))
                								.addGroup(gl_contentPane.createSequentialGroup()
                									.addPreferredGap(ComponentPlacement.RELATED)
                									.addComponent(lblLeaveBlankFor))
                								.addGroup(gl_contentPane.createSequentialGroup()
                									.addPreferredGap(ComponentPlacement.RELATED)
                									.addComponent(btnNewButton)))
                							.addGap(6)
                							.addGroup(gl_contentPane.createParallelGroup(Alignment.TRAILING)
                								.addGroup(gl_contentPane.createSequentialGroup()
                									.addGroup(gl_contentPane.createParallelGroup(Alignment.LEADING)
                										.addComponent(endDateField, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
                										.addComponent(lblEndDate))
                									.addGap(6)
                									.addGroup(gl_contentPane.createParallelGroup(Alignment.LEADING)
                										.addComponent(lblEndTime)
                										.addComponent(endTimeField, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)))
                								.addGroup(gl_contentPane.createParallelGroup(Alignment.LEADING)
                									.addComponent(rdbtnMonthly)
                									.addGroup(gl_contentPane.createSequentialGroup()
                										.addComponent(fridayCheckBox)
                										.addPreferredGap(ComponentPlacement.RELATED)
                										.addComponent(saturdayCheckBox))
                									.addGroup(gl_contentPane.createSequentialGroup()
                										.addGroup(gl_contentPane.createParallelGroup(Alignment.LEADING)
                											.addGroup(gl_contentPane.createSequentialGroup()
                												.addComponent(sundayCheckBox)
                												.addPreferredGap(ComponentPlacement.RELATED)
                												.addComponent(mondayCheckBox)
                												.addPreferredGap(ComponentPlacement.RELATED)
                												.addComponent(tuesdayCheckBox)
                												.addPreferredGap(ComponentPlacement.UNRELATED)
                												.addComponent(wednesdayCheckBox))
                											.addComponent(rdbtnWeekly))
                										.addPreferredGap(ComponentPlacement.RELATED)
                										.addComponent(thursdayCheckBox))
                									.addGroup(gl_contentPane.createSequentialGroup()
                										.addComponent(lblNewLabel_2)
                										.addPreferredGap(ComponentPlacement.UNRELATED)
                										.addComponent(intervalTextField, GroupLayout.PREFERRED_SIZE, 42, GroupLayout.PREFERRED_SIZE)
                										.addPreferredGap(ComponentPlacement.UNRELATED)
                										.addComponent(lblNewLabel_3))
                									.addComponent(lblRecurrencePattern)
                									.addComponent(rdbtnDaily)
                									.addComponent(rdbtnYearly))))))
                				.addGroup(gl_contentPane.createSequentialGroup()
                					.addContainerGap()
                					.addComponent(lblPriority)))
                			.addGap(107))
                );
                gl_contentPane.setVerticalGroup(
                	gl_contentPane.createParallelGroup(Alignment.LEADING)
                		.addGroup(gl_contentPane.createSequentialGroup()
                			.addGap(19)
                			.addGroup(gl_contentPane.createParallelGroup(Alignment.LEADING)
                				.addGroup(gl_contentPane.createSequentialGroup()
                					.addGap(3)
                					.addComponent(lblName))
                				.addComponent(eventField, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
                				.addGroup(gl_contentPane.createSequentialGroup()
                					.addGap(5)
                					.addComponent(lblTimezone))
                				.addGroup(gl_contentPane.createSequentialGroup()
                					.addGap(2)
                					.addComponent(comboBox, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)))
                			.addGroup(gl_contentPane.createParallelGroup(Alignment.LEADING)
                				.addGroup(gl_contentPane.createSequentialGroup()
                					.addGap(12)
                					.addGroup(gl_contentPane.createParallelGroup(Alignment.BASELINE)
                						.addComponent(lblStartTime)
                						.addComponent(lblStartTime_1)))
                				.addGroup(gl_contentPane.createSequentialGroup()
                					.addPreferredGap(ComponentPlacement.UNRELATED)
                					.addGroup(gl_contentPane.createParallelGroup(Alignment.BASELINE)
                						.addComponent(lblEndDate)
                						.addComponent(lblEndTime))))
                			.addGap(6)
                			.addGroup(gl_contentPane.createParallelGroup(Alignment.LEADING)
                				.addGroup(gl_contentPane.createParallelGroup(Alignment.BASELINE)
                					.addComponent(startTimeField, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
                					.addComponent(startDateField, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
                				.addGroup(gl_contentPane.createSequentialGroup()
                					.addGap(3)
                					.addComponent(lblNewLabel))
                				.addComponent(endDateField, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
                				.addComponent(endTimeField, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
                			.addGap(13)
                			.addGroup(gl_contentPane.createParallelGroup(Alignment.TRAILING)
                				.addGroup(gl_contentPane.createParallelGroup(Alignment.BASELINE)
                					.addComponent(lblCoordinates)
                					.addComponent(geoField, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
                				.addGroup(gl_contentPane.createSequentialGroup()
                					.addComponent(lblRecurrencePattern)
                					.addPreferredGap(ComponentPlacement.UNRELATED)))
                			.addGroup(gl_contentPane.createParallelGroup(Alignment.LEADING)
                				.addGroup(gl_contentPane.createSequentialGroup()
                					.addGap(9)
                					.addGroup(gl_contentPane.createParallelGroup(Alignment.BASELINE)
                						.addComponent(lblNewLabel_1)
                						.addComponent(locationField, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)))
                				.addGroup(gl_contentPane.createSequentialGroup()
                					.addPreferredGap(ComponentPlacement.RELATED)
                					.addComponent(rdbtnDaily)))
                			.addGroup(gl_contentPane.createParallelGroup(Alignment.LEADING)
                				.addGroup(gl_contentPane.createSequentialGroup()
                					.addGap(2)
                					.addGroup(gl_contentPane.createParallelGroup(Alignment.LEADING)
                						.addGroup(gl_contentPane.createSequentialGroup()
                							.addGap(14)
                							.addComponent(lblDescription)
                							.addGap(19)
                							.addComponent(lblPrivacy))
                						.addGroup(gl_contentPane.createSequentialGroup()
                							.addGap(6)
                							.addComponent(descriptionField, GroupLayout.PREFERRED_SIZE, 30, GroupLayout.PREFERRED_SIZE)
                							.addGap(7)
                							.addGroup(gl_contentPane.createParallelGroup(Alignment.LEADING)
                								.addComponent(rdbtnPublic)
                								.addComponent(rdbtnPrivate)
                								.addComponent(rdbtnConfidential)))))
                				.addGroup(gl_contentPane.createSequentialGroup()
                					.addGap(1)
                					.addGroup(gl_contentPane.createParallelGroup(Alignment.BASELINE)
                						.addComponent(lblNewLabel_2)
                						.addComponent(intervalTextField, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
                						.addComponent(lblNewLabel_3))
                					.addPreferredGap(ComponentPlacement.RELATED, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                					.addComponent(rdbtnWeekly)
                					.addPreferredGap(ComponentPlacement.RELATED)
                					.addGroup(gl_contentPane.createParallelGroup(Alignment.BASELINE)
                						.addComponent(sundayCheckBox)
                						.addComponent(mondayCheckBox)
                						.addComponent(tuesdayCheckBox)
                						.addComponent(wednesdayCheckBox)
                						.addComponent(thursdayCheckBox))))
                			.addPreferredGap(ComponentPlacement.RELATED)
                			.addGroup(gl_contentPane.createParallelGroup(Alignment.TRAILING)
                				.addGroup(gl_contentPane.createSequentialGroup()
                					.addGroup(gl_contentPane.createParallelGroup(Alignment.LEADING)
                						.addComponent(lblPriority)
                						.addComponent(rdbtnHigh)
                						.addComponent(rdbtnLow))
                					.addPreferredGap(ComponentPlacement.UNRELATED)
                					.addComponent(lblLeaveBlankFor)
                					.addGap(25))
                				.addGroup(gl_contentPane.createSequentialGroup()
                					.addGroup(gl_contentPane.createParallelGroup(Alignment.BASELINE)
                						.addComponent(fridayCheckBox)
                						.addComponent(saturdayCheckBox))
                					.addPreferredGap(ComponentPlacement.UNRELATED)
                					.addComponent(rdbtnMonthly)
                					.addGap(22)))
                			.addPreferredGap(ComponentPlacement.RELATED, 15, Short.MAX_VALUE)
                			.addGroup(gl_contentPane.createParallelGroup(Alignment.TRAILING)
                				.addComponent(rdbtnYearly)
                				.addComponent(btnNewButton))
                			.addContainerGap())
                );
                contentPane.setLayout(gl_contentPane);
        }
        /*
         * Tests to see that the start and end dates are valid entries
         * Returns true if the date is valid or false if it is not (ex. 30th of February)
         */
        boolean isValidDate(String dateToValidate, String dateFormat) {
          if(dateToValidate == null) {
            return false;
          }
         
          SimpleDateFormat sdf = new SimpleDateFormat(dateFormat);
          sdf.setLenient(false);
         
          try {
            Date date = sdf.parse(dateToValidate);
          }
          catch (ParseException e) {
            return false;
          }
          return true;
        }
       
        /*
         * Creates a new CalendarEvent Object with the given fields, calls the write method and then closes the GUI.
         */
        private void createFileButtonActionPerformed(ActionEvent bob){
          String eventName = eventField.getText();
          int startDate = 0;
          int endDate = 0;
          int startTime = 0;
          int endTime = 0;
          
          String tempString = geoField.getText();
          String[] tempArray = tempString.split(";");
          //System.out.println(tempArray.length);
          
          
                  
          
          try{
        	  startDate = Integer.parseInt(startDateField.getText());
        	  endDate = Integer.parseInt(endDateField.getText());
        	  startTime = Integer.parseInt(startTimeField.getText());
        	  endTime = Integer.parseInt(endTimeField.getText());
        	  
        	  //Checks to see that valid start date is inputed and the user has entered the information correctly
              if(isValidDate(startDateField.getText(), "yyyyMMdd") == false) {
                JOptionPane.showMessageDialog(null, "Start date is invalid", "Input Error", JOptionPane.ERROR_MESSAGE);
              }        	  
              //Checks to see that valid end date is inputed and the user has entered the information correctly
              else if(isValidDate(endDateField.getText(), "yyyyMMdd") == false) {
                JOptionPane.showMessageDialog(null, "End date is invalid", "Input Error", JOptionPane.ERROR_MESSAGE);
              }
              // Checks to see that a name has been given for the event
              else if(eventName.equals("")) {
                JOptionPane.showMessageDialog(null, "Please enter event name", "Input Error", JOptionPane.ERROR_MESSAGE);
              }
              // Checks to see that the start event day is earlier than end event day
              else if(startDate > endDate) {
                JOptionPane.showMessageDialog(null, "Start date is later than end date", "Input Error", JOptionPane.ERROR_MESSAGE);
              }
              // Checks to see that the start event time is earlier than end event time if under the same day
              else if((startDate == endDate) && (startTime > endTime)) {
                JOptionPane.showMessageDialog(null, "Start time is later than end time", "Input Error", JOptionPane.ERROR_MESSAGE);
              }
              // Checks to see that the times are not invalid
              else if(startTime > 2400 || endTime > 2400 || startTimeField.getText().length() < 4 || endTimeField.getText().length() < 4) {
                JOptionPane.showMessageDialog(null, "Invalid time (enter 0000 to 2400)", "Input Error", JOptionPane.ERROR_MESSAGE);
              }
              else if(!geoField.getText().contains(";")){
            	  JOptionPane.showMessageDialog(null, "Don't forget to seperate your lat and long with a \";\"", "Input Error", JOptionPane.ERROR_MESSAGE);
              }
              //checks to see if either daily or weekly is picked and if  they have values
              else if((rdbtnDaily.isSelected() && intervalTextField.getText().equals("")) || (rdbtnWeekly.isSelected() && !sundayCheckBox.isSelected() && !mondayCheckBox.isSelected() && !tuesdayCheckBox.isSelected() && !wednesdayCheckBox.isSelected()
        			  && !thursdayCheckBox.isSelected() && !fridayCheckBox.isSelected() && !saturdayCheckBox.isSelected()) ){
            	  JOptionPane.showMessageDialog(null, "Recurrence pattern incorret", "Input Error", JOptionPane.ERROR_MESSAGE);
              }
              else if(tempArray.length != 2){
            	  JOptionPane.showMessageDialog(null, "Please enter both lat and long values seperated by a ;", "Input Error", JOptionPane.ERROR_MESSAGE);
              }
              else if(Float.parseFloat(tempArray[0]) > 90 || Float.parseFloat(tempArray[0]) < -90 || Float.parseFloat(tempArray[1]) > 180 || Float.parseFloat(tempArray[1]) < -180){
            	  JOptionPane.showMessageDialog(null, "Please enter valid lat and long values", "Input Error", JOptionPane.ERROR_MESSAGE);
              }
              else{
                  //changes recurrence string to daily
                  if(rdbtnDaily.isSelected()){
                		  intervalRec += intervalTextField.getText();
                		  recurrenceString += "\r\nRRULE:" +"FREQ=" + freqType + ";" + intervalRec;
                	  
                  }
                  
                  //changes recurrence string to weekly
                  if(rdbtnWeekly.isSelected()){
                		  int morethanone = 0;
                		  // SO UGLY WILL FIX ONE DAY
                		  if(sundayCheckBox.isSelected()){
                			  dayRec += "SU";
                			  morethanone++;
                		  }
                		  if(mondayCheckBox.isSelected()){
                			 if(morethanone > 0){
                				 dayRec += ",";
                			 }
                			 dayRec += "MO";
                			 morethanone++;
                		  }
                		  if(tuesdayCheckBox.isSelected()){
                			 if(morethanone > 0){
                				 dayRec += ",";
                			 }
                			 dayRec += "TU";
                			 morethanone++;
                		  }

                		  if(wednesdayCheckBox.isSelected()){
                			 if(morethanone > 0){
                				 dayRec += ",";
                			 }
                			 dayRec += "WE";
                			 morethanone++;
                		  }
                		  if(thursdayCheckBox.isSelected()){
                			 if(morethanone > 0){
                				 dayRec += ",";
                			 }
                			 dayRec += "TH";
                			 morethanone++;
                		  }
                		  if(fridayCheckBox.isSelected()){
                			 if(morethanone > 0){
                				 dayRec += ",";
                			 }
                			 dayRec += "FR";
                			 morethanone++;
                		  }
                		  if(saturdayCheckBox.isSelected()){
                			 if(morethanone > 0){
                				 dayRec += ",";
                			 }
                			 dayRec += "SA";
                			 morethanone++;
                		  }
                		  
                		  //System.out.println(dayRec);
                		  recurrenceString += "\r\nRRULE:" + "FREQ=" + freqType + ";" + dayRec;
                		  //System.out.println(recurrenceString);
                		  
                		  
                	  
                  }
                  //changes recurrence string to monthly
                  if(rdbtnMonthly.isSelected()){
                	  String temp ="";
                	  String day = "";
                	  temp = startDateField.getText();
                	  day = temp.substring(6);
                	  recurrenceString += "\r\nRRULE:"+ "FREQ=" + freqType + ";" + "BYMONTHDAY=" + day;
                  }
                  //changes recurrence string to yearly
                  if(rdbtnYearly.isSelected()){
                	  String temp = "";
                	  String day = "";
                	  String month = "";
                	  temp  = startDateField.getText();
                	  month = temp.substring(4,6);
                	  day   = temp.substring(6);
                	  recurrenceString += "\r\nRRULE:" + "FREQ=" + freqType + ";" + "BYMONTHDAY=" + day + ";" + "BYMONTH=" + month;

                  }
                  
                  
               CalendarEvent testCalendarEvent = new CalendarEvent(eventField.getText(), startDateField.getText(), endDateField.getText(),startTimeField.getText(),
                                                    endTimeField.getText(),(String)comboBox.getSelectedItem(), descriptionField.getText(), locationField.getText(),
                                                    privacyType, priority, recurrenceString, geoField.getText());
               testCalendarEvent.write();
               this.dispose();
              }
	      }
          catch(NumberFormatException e){
        	   JOptionPane.showMessageDialog(null, "NumberFormatException", "Input Error", JOptionPane.ERROR_MESSAGE);
          }         
        }
}
